﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Input;
using Acr.XamForms.UserDialogs;
using SpotOffer.AppConstants;
using SpotOffer.CustomViews;
using SpotOffer.DataModels;
using SpotOffer.Models;
using SpotOffer.Resource;
using SpotOffer.Services;
using SpotOffer.Utils;
using SpotOffer.View;
using Xamarin;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;

namespace SpotOffer.ViewModels
{
    public class MenuViewModel : BaseViewModel
    {
        private List<Category> _categoryList;
        private CityCategory _cityCategory;
        private List<City> _cityList;
        private List<Deal> _dealList;
        private Category _selectedCategory;




        public override void OnViewAppearing()
        {
            base.OnViewAppearing();
            GetMenu();
        }			
	
        private void GetMenu()
        {
            App.AppInstance.MasterPage.IsPresentedChanged += (s, e) =>
            {
                if (((MasterDetailPage) s).IsPresented)
                {
                    Debug.WriteLine("((MasterDetailPage) s).IsPresented");
                    GetCity();
                }
               
            };
            if (App.Cache.Get<City>(CacheKeys.City) != null)
            {
                GetCity();
            }
        }


        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                NotifyPropertyChanged();
            }
        }

        private string _title;
        private City cachedCity;
        private City currentCity;

        public CityCategory CityCategory
        {
            get { return _cityCategory; }
            set
            {
                _cityCategory = value;
                NotifyPropertyChanged();
            }
        }

        public List<Category> CategoryList
        {
            get { return _categoryList; }
            set
            {
                if (value != null)
                {
                    _categoryList = value;


                    NotifyPropertyChanged();
                }
            }
        }

        public List<City> CityList
        {
            get { return _cityList; }
            set
            {
                if (value != null)
                {
                    _cityList = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public List<Deal> DealList
        {
            get { return _dealList; }
            set
            {
                if (value != null)
                {
                    _dealList = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public Category SelectedCategory
        {
            get { return _selectedCategory; }
            set
            {
                var cachedCity = App.Cache.Get<City>(CacheKeys.City);
                if (cachedCity != null & value != null & value != _selectedCategory)
                {
                    //----------------------Styling---------------//
                    if (_selectedCategory != null)
                    {
                        _selectedCategory.Higlighted = false;
                    }
                    _selectedCategory = value;
                    _selectedCategory.Higlighted = true;
                    //----------------------Styling---------------
                    Insights.Track("CategoryClicked", new Dictionary<string, string> { { "CategoryClicked", _selectedCategory.categoryName }, });
                }
            }
        }

        public async void GetAllCities()
        {
            Dialogs.ShowLoading(AppResources.Loading);
            try
            {
                CityCategory = await DependencyService.Get<ICityCategoryService>().GetCitiesCategories();
                CityList = new List<City>(CityCategory.cities);
            }
            catch (Exception e)
            {
                Dialogs.HideLoading();
                var ac = new AlertConfig { Message = e.Message };
                Dialogs.Alert(ac);
            }
            finally
            {
                Dialogs.HideLoading();
            }
        }

        public async void GetCity()
        {
           // Dialogs.ShowLoading(AppResources.Loading);
            try
            {
                var cachedCity = App.Cache.Get<City>(CacheKeys.City);
                var templist = await DependencyService.Get<IDealListService>().GetCity(cachedCity.id);
                templist.categories.RemoveAll(c => c.count == 0);

                if (DealList == null || DealList.Count != templist.deals.Count)
                {
                    foreach (var category in templist.categories)
                    {
                        category.categoryName = category.name + " (" + category.count + ")";
                    }

                    DealList = templist.deals;

                    //category all
                    var categoryAll = new Category
                    {
                        id = "all",
                        name = AppResources.All,
                        categoryName = AppResources.All + " (" + templist.deals.Count + ")",
                        count = templist.deals.Count
                    };
                    templist.categories.Insert(0, categoryAll);
                    CategoryList = templist.categories;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e + " " + e.Message);
                Dialogs.HideLoading();
                var ac = new AlertConfig { Message = e.Message };
                Dialogs.Alert(ac);
            }
            finally
            {
              //  Dialogs.HideLoading();
            }
        }
    }
}